package com.snail.common.dyd.impl;

import com.snail.common.dyd.DataBaseOperator;
import com.snail.common.dyd.DataBaseTypeSwitcher;
import com.snail.common.dyd.DynamicDataBase;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;

@Data
@Slf4j
@AllArgsConstructor
public class AspectDataBaseTypeSwitcher implements DataBaseTypeSwitcher {

    private ProceedingJoinPoint proceedingJoinPoint;
    @Override
    public void switchDataBaseType(DataBaseOperator dataBaseOperator) {
        Object target = getProceedingJoinPoint().getTarget();
        if(!(target instanceof DynamicDataBase)){
            log.error("指定的对象不是DynamicDataBase，不自动切换数据源");
            return ;
        }
        if(target instanceof DynamicDataBase){
            ((DynamicDataBase) target).setDataBaseOperator(dataBaseOperator);
        }
    }
}
